package bcontrollers

import (
	"encoding/json"

	"advertoper/banner_manage/bservice"
	"advertoper/controllers"
	"advertoper/errorcode"
)

type BannerDataController struct {
	controllers.URLSignController
}

//添加banner图片
// @router /add [post]
func (p *BannerDataController) BannerAdd() {
	defer p.Output("BannerAdd")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &bservice.BannerDataAdd{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := bservice.GetBannerManageService().BannerAdd(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

//修改banner图片
// @router /update [post]
func (p *BannerDataController) BannerUpdate() {
	defer p.Output("BannerUpdate")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &bservice.BannerDataUpdate{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := bservice.GetBannerManageService().BannerUpdate(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

//web查询
// @router /list [get]
func (p *BannerDataController) BannerList() {
	defer p.Output("BannerList")
	p.ForceHTTPS()

	status, _ := p.GetInt("status", 1)
	serverCode := p.GetString("server_code", "")
	data := bservice.GetBannerManageService().BannerWebList(status, serverCode)
	if len(data) == 0 {
		p.Resp["code"] = 101
		p.Resp["msg"] = "no data"
	} else {
		p.Resp["code"] = 0
		p.Resp["msg"] = "success"
		p.Resp["data"] = data
	}

}

//添加运营位控制
// @router /controller/add [post]
func (p *BannerDataController) ControllerAdd() {
	defer p.Output("ControllerAdd")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &bservice.BannerControllerReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := bservice.GetBannerControllerService().ControllerAdd(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

//修改运营位控制
// @router /controller/update [post]
func (p *BannerDataController) ControllerUpdate() {
	defer p.Output("ControllerUpdate")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &bservice.BannerControllerReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := bservice.GetBannerControllerService().ControllerUpdate(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}

//查询所有或单个运营位控制
// @router /controller/list [get]
func (p *BannerDataController) ControllerList() {
	defer p.Output("ControllerUpdate")
	p.ForceHTTPS()

	serverCode := p.GetString("server_code", "all")
	code, msg, data := bservice.GetBannerControllerService().ControllerList(serverCode)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == 0 {
		p.Resp["data"] = data
	}
}
